/**
 * 全局插件配置资源（vue插件部分）
 * @module utils/plugin
 */
import * as ElIcons from '@element-plus/icons-vue';
import draggable from './directives/draggable';
import hasPermi from './directives/hasPermi';
import waterMarker from './directives/waterMarker';
import waves from './directives/waves';
import clipboard from './directives/clipboard';
import showTip from './directives/showTip';
import router from '@/router';

/**方法 */
import { selectDictLabel, resetForm } from './util';

/**
 * 模块store数据
 */
import Com from '@s/com';
import Dict from '@s/dict';
import Guarder from '@s/guarder';
import User from '@s/user';

export default function (app) {
	// svg字体图标
	const svgModules = import.meta.glob('../assets/svg/*.svg');
	for (const key of Object.keys(svgModules)) {
		User().svgIcons.push(/^(?:\.\.\/assets\/svg\/)(\S+)(?:\.svg)/gi.exec(key).at(1));
	}
	for (const [key, component] of Object.entries(ElIcons)) {
		app.component(key, component);
		User().ElIcons.push(key);
	}
	/**
	 * 全局指令
	 */
	app.directive('draggable', draggable);
	app.directive('hasPermi', hasPermi);
	app.directive('waterMarker', waterMarker);
	app.directive('waves', waves);
	app.directive('clipboard', clipboard);
	app.directive('showTip', showTip);
	/**
	 * 原型链全局挂载方法
	 */
	app.config.globalProperties.$router = router;
	window.$store = app.config.globalProperties.$store = { com: Com(), dict: Dict(), guarder: Guarder(), user: User() };

	app.config.globalProperties.selectDictLabel = selectDictLabel;
	app.config.globalProperties.resetForm = resetForm;

	/**
	 * vite动态加载图片静态文件
	 * @param {string} path 文件路径
	 * @returns
	 */
	const modules = import.meta.glob('../assets/img/**/*', { eager: true });
	app.config.globalProperties.getImgUrl = (path) => {
		if (path && modules) {
			if (path.includes('../assets/img/')) {
				return modules[path].default;
			} else {
				return modules[`../assets/img/${path}`].default;
			}
		}
	};
}
